package com.block.datapush.form.item;

import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.service.LogFactory;
import com.block.datapush.form.enums.PushResultEnum;
import com.block.datapush.form.item.util.JinRiFenQiUtil;
import com.block.datapush.form.service.IFormPushHandle;
import com.block.datapush.model.dto.FormPushResultDTO;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 今日分期
 *
 */
@Service
public class JinRiFenQiPushHandleService implements IFormPushHandle {

    private static final Logger log = LogFactory.getPushLog();

    @Override
    public boolean queryMatchResult(FormInfo formInfo, MemberExtInfo memberExtInfo) throws Exception {

            String queryUrl = formInfo.getQueryUrl();
            String mobileMd5 = SecureUtil.md5(memberExtInfo.getMobile()).toLowerCase();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("phoneMd5", mobileMd5);

            log.info("【今日分期】撞库开始【queryUrl】{}【paramMap】{}",
                    queryUrl,  JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(queryUrl)
                    .header("Content-Type", "application/json")
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
            log.info("【今日分期】撞库结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);
            Integer code = resultObject.getInteger("code");

            if (StrUtil.equals("500", String.valueOf(code))){
                    return true;
            }
            return false;
    }

    /**
     * @param formInfo
     * @param memberExtInfo
     * @param orderNo
     * @param jsonObject
     * @return
     */
    @Override
    public FormPushResultDTO pushMemberInfo(FormInfo formInfo, MemberExtInfo memberExtInfo, String orderNo, JSONObject jsonObject) throws Exception {

        FormPushResultDTO pushResultDTO = new FormPushResultDTO();
        pushResultDTO.setOrderNo(orderNo);

            String queryUrl = formInfo.getRequestUrl();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("channel", formInfo.getApiKey());
            paramMap.put("phone", memberExtInfo.getMobile());
            paramMap.put("idnum", memberExtInfo.getIdCard());
            paramMap.put("city", memberExtInfo.getCity());
            paramMap.put("name", memberExtInfo.getUsername());
            paramMap.put("loan", BaseTypeInfoEnum.LoanAmount.getEntryByValue(memberExtInfo.getLoanAmount()).getAmount());
            paramMap.put("age", memberExtInfo.getAge());

            paramMap.put("loan_term", 3);
            paramMap.put("loan_use", 8);
            paramMap.put("professional", 1);
            paramMap.put("enterprise_type", 1);
            paramMap.put("monthly_income", 3);
            paramMap.put("salary_type", 1);
            paramMap.put("enterprise_flow", 1);
            paramMap.put("enterprise_license", 1);

            paramMap.put("credit_history", JinRiFenQiUtil.getYuqi(memberExtInfo.getYuqi()));
            paramMap.put("zhima_credit", JinRiFenQiUtil.getZhima(memberExtInfo.getZhimaScore()));
            paramMap.put("has_fund", JinRiFenQiUtil.getGjjSheBao(memberExtInfo.getGjj(), memberExtInfo.getSocialInsurance()));
            paramMap.put("micro_loan", 2);
            paramMap.put("life_insurance", JinRiFenQiUtil.getBaoDan(memberExtInfo.getInsurance()));
            paramMap.put("house_property", JinRiFenQiUtil.getHouse(memberExtInfo.getHouse()));
            paramMap.put("car_property", JinRiFenQiUtil.getCar(memberExtInfo.getCar()));
            paramMap.put("huabei", JinRiFenQiUtil.getHuabei(memberExtInfo.getHuabei()));
            paramMap.put("baitiao", JinRiFenQiUtil.getBaitiao(memberExtInfo.getBaitiao()));
            paramMap.put("car_type", JinRiFenQiUtil.getCar(memberExtInfo.getCar()));


            log.info("【今日分期】导单开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
            String result = HttpUtil.createPost(queryUrl)
                    .header("Content-Type", "application/json")
                    .body(JSON.toJSONString(paramMap))
                    .execute().body();
            log.info("【今日分期】导单结束【result】{}", result);

            JSONObject resultObject = JSON.parseObject(result);
            Integer code = resultObject.getInteger("code");
            String status = resultObject.getString("status");
            String msg = resultObject.getString("msg");

            pushResultDTO.setResult(result);
            pushResultDTO.setCode(String.valueOf(status));
            pushResultDTO.setMessage(UnicodeUtil.toString(msg));
            pushResultDTO.setParamContent(JSON.toJSONString(paramMap));

            if (ObjectUtil.equal(code, Integer.valueOf(500))){
                pushResultDTO.setPushResult(PushResultEnum.SUCCESS.getType());
                pushResultDTO.setSuccess(true);
            }else{
                pushResultDTO.setPushResult(PushResultEnum.FAILED.getType());
                pushResultDTO.setSuccess(false);
            }
            return pushResultDTO;

    }

    @Override
    public void afterPushHandle(FormInfoHis formInfoHis) {

    }

}
